package com.alexsun.registerview

import android.annotation.SuppressLint
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.Button
import android.widget.CheckBox
import android.widget.EditText
import android.widget.RadioButton
import android.widget.Spinner
import android.widget.TextView

class MainActivity : AppCompatActivity(), AdapterView.OnItemSelectedListener, View.OnClickListener {

    private var country = ""
    private var area = ""

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val spinnerCountry: Spinner = findViewById(R.id.spinner_country)
        val spinnerArea: Spinner = findViewById(R.id.spinner_area)
        spinnerArea.prompt = "选择地区"
        // 创建数组适配器 ArrayAdapter 并指定默认布局
        ArrayAdapter.createFromResource(
            this,
            R.array.country_array,
            android.R.layout.simple_spinner_item
        ).also { adapter ->
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
            spinnerCountry.adapter = adapter
        }
        ArrayAdapter.createFromResource(
            this,
            R.array.area_china,
            android.R.layout.simple_spinner_item
        ).also { adapter ->
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
            spinnerArea.adapter = adapter
        }
        spinnerCountry.onItemSelectedListener = this
        spinnerArea.onItemSelectedListener = this

        val registerBtn: Button = findViewById(R.id.button)
        registerBtn.setOnClickListener(this)
    }

    override fun onItemSelected(parent: AdapterView<*>, view: View?, pos: Int, id: Long) {
        val itemValue = parent.selectedItem.toString()
        val spinnerArea: Spinner = findViewById(R.id.spinner_area)
        when (parent.id) {
            R.id.spinner_area -> {
                area = itemValue
            }
            R.id.spinner_country -> {
                country = itemValue
                when (pos) {
                    0 -> {
                        ArrayAdapter.createFromResource(
                            this,
                            R.array.area_china,
                            android.R.layout.simple_spinner_item
                        ).also { adapter ->
                            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
                            spinnerArea.adapter = adapter
                        }
                    }
                    1 -> {
                        ArrayAdapter.createFromResource(
                            this,
                            R.array.area_usa,
                            android.R.layout.simple_spinner_item
                        ).also { adapter ->
                            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
                            spinnerArea.adapter = adapter
                        }
                    }
                    2 -> {
                        ArrayAdapter.createFromResource(
                            this,
                            R.array.area_jp,
                            android.R.layout.simple_spinner_item
                        ).also { adapter ->
                            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
                            spinnerArea.adapter = adapter
                        }
                    }
                }
            }
        }
    }

    override fun onNothingSelected(parent: AdapterView<*>) {
    }

    @SuppressLint("SetTextI18n")
    override fun onClick(p0: View?) {
        val username = findViewById<EditText?>(R.id.editTextTextPersonName).text.toString()
        val password = findViewById<EditText?>(R.id.editTextTextPassword).text.toString()
        val ganderRadioMale: RadioButton = findViewById(R.id.radioButton)
        val gander = if (ganderRadioMale.isChecked) "男" else "女"
        val resText: TextView = findViewById(R.id.res_text)
        val hobbyList = arrayListOf<String>()
        if (findViewById<CheckBox>(R.id.checkBox).isChecked) {
            hobbyList.add("足球")
        }
        if (findViewById<CheckBox>(R.id.checkBox2).isChecked) {
            hobbyList.add("篮球")
        }
        if (findViewById<CheckBox>(R.id.checkBox3).isChecked) {
            hobbyList.add("台球")
        }
        if (findViewById<CheckBox>(R.id.checkBox4).isChecked) {
            hobbyList.add("网球")
        }
        val hobby = hobbyList.joinToString("、")
        resText.text = """
            用户名：$username
            密码：$password
            性别：$gander
            国家：$country
            地区：$area
            爱好：$hobby
        """.trimIndent()
    }
}